package com.rt.easyjava.base.modules.timerjob.listen;


import com.rt.easyjava.base.modules.timerjob.config.SchedulerConfig;
import com.rt.easyjava.base.modules.timerjob.service.job.MyQuartzJobBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description:
 * 利用Quartz定时任务，可以在初始化上实现，
 * 初始化触发器等信息，这里通过Listener初始化
 */
//@Component
@Slf4j
public class StartApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
	@Autowired
	SchedulerConfig schedulerConfig;

	public static AtomicInteger count = new AtomicInteger(0);
	private static String TRIGGER_GROUP_NAME="test_trriger";
	private static String JOB_GROUP_NAME ="test_job";

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		// 防止重复执行
		System.out.println("==========================spring容器初始化完毕======================");
		System.out.println("ssssres11:"+ event.getApplicationContext().getParent() );
		System.out.println("ssssres11:"+ (event.getApplicationContext().getParent() == null));
		if (/*event.getApplicationContext().getParent() == null && */count.incrementAndGet() <= 1) { //root application context 没有parent，他就是老大.
			//需要执行的逻辑代码，当spring容器初始化完成后就会执行该方法。
			initMyJob();
		}
	}

	public void initMyJob() {
		//调度器
		Scheduler scheduler = null;
		try {
			log.info("创建scheduler");
			//创建scheduler
			scheduler = schedulerConfig.scheduler();
			//定义一个TriggerKey
			TriggerKey triggerKey = TriggerKey.triggerKey("trigger1", TRIGGER_GROUP_NAME);
			CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
			if (null == trigger) {
				Class clazz = MyQuartzJobBean.class;
				//定义一个JobDetail,其中的定义Job类，是真正的执行逻辑所在
				JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity("job1", JOB_GROUP_NAME).build();
				CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/6 * * * * ? *");
				//定义一个Trigger
				trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", TRIGGER_GROUP_NAME)
						.withSchedule(scheduleBuilder).build();
					scheduler.scheduleJob(jobDetail, trigger);
				log.info("Quartz 创建了job:...:{}",jobDetail.getKey());
			} else {
				log.info("job已存在:{},{}",trigger.getKey(), trigger.getCronExpression());
			}

			/*//定义一个TriggerKey
			TriggerKey triggerKey2 = TriggerKey.triggerKey("trigger2", TRIGGER_GROUP_NAME);
			CronTrigger trigger2 = (CronTrigger) scheduler.getTrigger(triggerKey2);
			if (null == trigger2) {
				Class clazz = MyQuartzJobBean2.class;
				//定义一个JobDetail,其中的定义Job类，是真正的执行逻辑所在
				JobDetail jobDetail2 = JobBuilder.newJob(clazz).withIdentity("job2", JOB_GROUP_NAME).build();
				CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/50 * * * * ?");
				//定义一个Trigger
				trigger2 = TriggerBuilder.newTrigger().withIdentity("trigger2", TRIGGER_GROUP_NAME)
						.withSchedule(scheduleBuilder).build();
				scheduler.scheduleJob(jobDetail2, trigger2);
				log.info("Quartz 创建了job:...:{}",jobDetail2.getKey());
			} else {
				log.info("job已存在:{}",trigger2.getKey());
			}*/
			scheduler.start();
		} catch (Exception e) {
			log.info(ExceptionUtils.getFullStackTrace(e));
		}
	}
}
